home *** CD-ROM | disk | FTP | other *** search
- /*
- File: UniversalConfigParse.c
-
- Contains: xxx put contents here xxx
-
- Version: xxx put version here xxx
-
- Copyright: © 1998 by Apple Computer, Inc., all rights reserved.
-
- */
-
- #include <Types.h>
- #include <Devices.h>
- #include <processes.h>
- #include <DriverServices.h>
-
- #include <USB.h>
-
- #include "UniversalModule.h"
-
- // we need to find the interface descriptor and everything it owns
- // since the interface descriptor we were passed may not have had
- // the other descriptors following it. We go to the config
- // descriptor, and look forward until we find the interface with
- // the number we are looking for. we know that starting from that
- // point are all our endpoint descriptors
- OSErr FindHIDInterfaceByInterfaceNumber(LogicalAddress pConfigDesc, UInt32 interfaceNumber, USBInterfaceDescriptorPtr * hInterfaceDesc)
- {
- UInt32 totalLength;
- void * pEndOfDescriptors;
- USBInterfaceDescriptorPtr pMyIntDesc;
- USBDescriptorHeaderPtr pCurrentDesc;
- unsigned long anAddress, anOffset;
-
- totalLength = ((USBConfigurationDescriptorPtr)pConfigDesc)->totalLength;
- pEndOfDescriptors = (Ptr)pConfigDesc + totalLength; // get the total length and add it to the start of the config space
- pCurrentDesc = (USBDescriptorHeaderPtr)pConfigDesc; // point the currentdesc to the start of the config space
-
- while (pCurrentDesc < pEndOfDescriptors) // as long as we haven't exhausted all the descriptors
- {
- if (pCurrentDesc->descriptorType == kUSBInterfaceDesc) // look at the current descriptor
- {
- pMyIntDesc = (USBInterfaceDescriptorPtr)pCurrentDesc; // if it's an interface descriptor
- if ((pMyIntDesc->interfaceNumber == interfaceNumber)) // see if it's the interface number we want...
- {
- *hInterfaceDesc = pMyIntDesc; // if it is, then return with hInterfaceDesc set to the
- return noErr; // current descriptor pointer
- }
- }
-
- anAddress = (unsigned long) pCurrentDesc; // Nope, that either wasn't an interface descriptor
- anOffset = (unsigned long) pCurrentDesc->length;
- anAddress += anOffset;
- pCurrentDesc = (USBDescriptorHeaderPtr) anAddress;
-
- if (pCurrentDesc->length == 0)
- break;
- } // or it was, but not the droid we're looking for.
- *hInterfaceDesc = NULL;
- return kUSBInternalErr;
- }
-
-
- // this returns a pointer to the first HID descriptor in the configuration
- // _and_ all the HID report and HID Physical descripters follow it
- // the caller has to be smart enough to parse it correctly, and
- // to know not to read too far (as any other configurations with their
- // corresponding HID descriptors) might follow
- OSErr GetHIDDescriptors(USBConfigurationDescriptorPtr pConfigDesc, USBHIDDescriptorPtr * hHIDDesc)
- {
- UInt32 totalLength;
- void * pEndOfDescriptors;
- USBDescriptorHeaderPtr pCurrentDesc;
- unsigned long anAddress, anOffset;
-
- // clear return value, in case we fail (should we do this, or leave it unchanged?)
- *hHIDDesc = nil;
-
- totalLength = pConfigDesc->totalLength;
-
- pEndOfDescriptors = (Ptr)pConfigDesc + totalLength;
- pCurrentDesc = (USBDescriptorHeaderPtr)pConfigDesc;
-
- if (pCurrentDesc)
- {
- if (pCurrentDesc->descriptorType == kUSBInterfaceDesc) // make certain pInterfaceDesc points to an interface
- { // it does...
-
- anAddress = (unsigned long) pCurrentDesc; // Nope, point to the next descriptor
- anOffset = (unsigned long) pCurrentDesc->length;
- anAddress += anOffset;
- pCurrentDesc = (USBDescriptorHeaderPtr) anAddress; // point to the next descriptor
-
- while (pCurrentDesc < pEndOfDescriptors) // and as long as we don't go past the end
- { //
- if (pCurrentDesc->descriptorType == kUSBInterfaceDesc) // if we find *another* interface descriptor
- return kUSBInternalErr; // then bail.
- else
- {
- if (pCurrentDesc->descriptorType == kUSBHIDDesc) // if we find a HID descriptor (we'll return the 1st one) •••
- {
- *hHIDDesc = (USBHIDDescriptorPtr)pCurrentDesc;
- return noErr; // then return a pointer to it.
- }
- }
- }
- }
- }
-
- return kUSBInternalErr;
- }
-